import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import 'element-plus/dist/index.css'
import { getNoRootUrl } from './common'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
// 配置中文的
const app = createApp(App)
app.use(ElementPlus, {
  locale: zhCn,
})
import NProgress from 'nprogress' // 引入nprogress样式
import 'nprogress/nprogress.css' // 引入nprogress样式
let url = new URL(window.location.href)
let params = new URLSearchParams(url.search)
import piniaPersistedState from 'pinia-plugin-persistedstate'
app.use(createPinia().use(piniaPersistedState))
import { useConfigStore, useUserStore } from '@/stores'
const useConfig = useConfigStore()
const userInfo = useUserStore()
app.use(router)
//无需判断的路由数组 路由鉴权拦截
let goUrls = ['guide', 'login', 'autologin'] // 路由拦截
router.beforeEach((to: any, from, next) => {
  // 路由拦截
  // console.log('NProgress', NProgress);
  // console.log('router', to, from);
  // console.log("token", userInfo.token);
  NProgress.start() // 开启进度条
  if (userInfo.token) {
    next()
  } else {
    // //字符串大写转小写
    const noLoginUrls = (useConfig.config.noLoginUrls || []).map((item: string) =>
      item.toLowerCase(),
    )
    goUrls = goUrls.concat(noLoginUrls)
    if (goUrls.includes(to.name)) {
      next()
    } else {
      //弹出一个提示信息
      next({
        path: '/login',
        query: { redirect: to.fullPath },
      })
    }
  }
  //获取配置文件信息
  if (to.name == 'login') {
    getNoRootUrl('public/config.json?t=' + Math.random())
      .then((config) => {
        // console.log("config", config.data,Object.prototype.toString.call(config.data));NProgress.start(); // 开启进度条

        if (
          config.data == null ||
          Object.prototype.toString.call(config.data) !== '[object Object]'
        ) {
          console.log('config文件没有配置')
        }
      })
      .catch((error) => {
        console.log('config文件没有配置')
      })
      .finally(() => {
        next()
      })
  }
})
router.afterEach((to, from) => {
  // if (!dsf.config.login.isHiddenLoading) {
  NProgress.done() // 关闭进度条
  // }
})

//根据 MODE 来判断
if (import.meta.env.MODE === 'development') {
  // 开发环境下的代码
  console.log('当前是开发环境');
} else if (import.meta.env.MODE === 'production') {
  // 生产环境下的代码
  console.log('当前是生产环境');
}
//svg 配置
import 'virtual:svg-icons-register'
//引入全局样式
import '@/styles/index.scss'
//引入暗黑模式的切换
import 'element-plus/theme-chalk/dark/css-vars.css'
// 注册整个项目的全局组件
import components from '@/components/'
app.use(components)
app.mount('#app')
